/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rosic.mapCanvas;

import com.sun.lwuit.Component;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.plaf.Style;

public class ImageComponent extends Component {

    private int width;
    private int height;
    private boolean isScrollableX;
    private boolean isScrollableY;
    private String text;
    private Image image;

    public ImageComponent(String text) {
        this.text = text;
        
        // this.setWidth(5000);
        // this.setHeight(5000);
         //this.setX(0);
         //this.setY(0);
        setHandlesInput(true);
        setFocusPainted(false);
        getStyle().setBorder(null);
        setIsScrollVisible(true);
        isScrollableX=true;
        isScrollableY=true;
    }

    public void paint(Graphics g) {
        System.out.println("llama a paint");
        Style style = getStyle();
        if (image != null) {
            int xx = getX();
            int yy = getY();
            if (isScrollableX || isScrollableY) {
                g.clipRect(xx/*+style.getPadding(LEFT)+style.getMargin(LEFT)*/,
                        yy/*+style.getPadding(TOP)+style.getMargin(TOP)*/,
                        width + getScrollX(), height + getScrollY());
            }
            g.drawImage(image, xx, yy);
        } else {
            Font font = style.getFont();
            g.setFont(font);
            int color = style.getFgColor();
//            if(hasFocus())
//                color = style.getFgSelectionColor();
            g.setColor(color);
            g.drawString(text, (getWidth() - font.stringWidth(text)) >> 1,
                    getHeight() >> 1);
        }
    }

    public void keyPressed(int keyCode) {
        if (isScrollableX == false && isScrollableY == false) {
            super.keyPressed(keyCode);
            return;
        }

        int gameAction = Display.getInstance().getGameAction(keyCode);
        int step = 5;
        int newScrollX = getScrollX();
        int newScrollY = getScrollY();
        switch (gameAction) {
            case Display.GAME_UP:
                newScrollY -= step;
                break;
            case Display.GAME_DOWN:
                newScrollY += step;
                break;
            case Display.GAME_LEFT:
                newScrollX -= step;
                break;
            case Display.GAME_RIGHT:
                newScrollX += step;
                break;
            default:
                super.keyPressed(keyCode);
                return;
        }
        if (newScrollX < 0) {
            newScrollX = 0;
        }
        if (newScrollY < 0) {
            newScrollY = 0;
        }
        if (newScrollX + width > image.getWidth()) {
            newScrollX = image.getWidth() - width;
        }
        if (newScrollY + height > image.getHeight()) {
            newScrollY = image.getHeight() - height;
        }
        if (newScrollX != getScrollX()) {
            setScrollX(newScrollX);
        }
        if (newScrollY != getScrollY()) {
            setScrollY(newScrollY);
        }
    }

    public boolean isScrollableX() {
        return isScrollableX;
    }

    public boolean isScrollableY() {
        return isScrollableY;
    }

    public void setImage(Image image) {
        this.image = image;
//        Style style = getStyle();
        width = getWidth()/*-style.getPadding(LEFT)-style.getPadding(RIGHT)*/;
        isScrollableX = width < image.getWidth();
        height = getHeight()/*-style.getPadding(TOP)-style.getPadding(BOTTOM)*/;
        isScrollableY = height < image.getHeight();
        // !!! after setting both isScrollableX and isScrollableY !!!
        if (isScrollableX) {
            width -= getSideGap();
            setScrollX((image.getWidth() - width) >> 1);
        }
        if (isScrollableY) {
            height -= getBottomGap();
            setScrollY((image.getHeight() - height) >> 1);
        }
        setScrollSize(new Dimension(image.getWidth(), image.getHeight()));
    }
}
